package com.wthink.hzgj.service

import com.alibaba.fastjson.JSON
import com.wthink.hzgj.busplan.`interface`.ListCallBack
import com.wthink.hzgj.service.jt808.*
import com.wthink.hzgj.util.Log
import com.wthink.hzgj.util.PreferencesUtils
import com.wthink.hzgj.util.`interface`.ProgrossCallBack
import com.wthink.hzgj.util.decodeBase64ToByteArray
import com.wthink.hzgj.util.encodeBase64ToString

object JT808Help {
//    var simNo: String = ""

    lateinit var listCallBack : ListCallBack

    fun setlistCallBack(callBack: ListCallBack) {
        listCallBack = callBack
    }

    private fun makeJT808Message(type: String): JT808Message? {
        return when (type.toUpperCase()) {
            "0B02" -> JT_0B02()
            "8B01" -> JT_8B01()
            "8B07" -> JT_8B07()
            "8300" -> JT_8300()
            "4600" -> JT_4600()
            "4800" -> JT_4800()
            "4801" -> JT_4801()
            "8B09" -> JT_8B09()
            "8B03" -> JT_8B03()
            "5000" -> JT_5000()
            else -> null
        }
    }

    fun decode(type: String, payload: String): JT808Message? {
        val message = makeJT808Message(type)
        if (message == null) {
//            if (simNo.isEmpty()) {
//                simNo = parseSimNo(payload)
//            }
            Log.debug("type:${type} 不处理")
            return null
        }
        if (message is JT_8300) {
            message.notice = payload
            android.util.Log.d("jt8300000",payload)
            //val byteArray = payload.decodeBase64ToByteArray()
            //message.readFromBytes(byteArray)
            //message.readFromBytes(payload.decodeBase64ToByteArray())
        } else if (message is JT_4600) {
            message.isFaced = payload
        } else if (message is JT_4800) {
            message.isUSBadded = payload
        }else if (message is JT_4801) {
            message.process = payload
        } else if(message is JT_8B07){
            val byteArray = payload.decodeBase64ToByteArray()
            message.readFromBytes(byteArray)
            val arrayList = message.arrayList
            val date = message.yysj
            val boolean = PreferencesUtils.getBoolean("isSelf")
            if (boolean) {
                if (null != listCallBack) {
                    listCallBack.callList(arrayList,date)
                }
            }
        } else if (message is JT_5000) {
            message.speedMsg = payload
        } else {
            val byteArray = payload.decodeBase64ToByteArray()
            message.readFromBytes(byteArray)
        }
        return message
//        val protocol = JT808Protocol()
//        protocol.readFromBytes(payload.decodeBase64ToByteArray())
//        message.simNo = protocol.simNo
//        if (simNo.isEmpty()) {
//            simNo = protocol.simNo!!
//        }
//        message.messageSerialNo = protocol.messageSerialNo
//        if (protocol.messageBody != null) {
//            message.readFromBytes(protocol.messageBody!!)
//        }
//        return message
    }

    private fun parseSimNo(payload: String): String {
        val protocol = JT808Protocol()
        protocol.readFromBytes(payload.decodeBase64ToByteArray())
        return protocol.simNo!!
    }

    fun encode(message: JT808Message): String {
        val encodeBase64ToString = message.writeToBytes().encodeBase64ToString()
        android.util.Log.d("encode报文",encodeBase64ToString)
        return encodeBase64ToString

       /* if (message is JT_0B09) {
            return JSON.toJSONString(message).encodeBase64ToString()
        } else {
            return message.writeToBytes().encodeBase64ToString()
        }*/

//        val protocol = JT808Protocol()
//        protocol.messageSerialNo = message.messageSerialNo
//        protocol.messageType = message.messageType
//        protocol.isSubPacket = false
//        protocol.simNo = message.simNo
//        protocol.messageBody = message.writeToBytes()
//
//        return protocol.writeToBytes().encodeBase64ToString()
    }

    fun encode0b09(message: JT808Message): String {
        return JSON.toJSONString(message).encodeBase64ToString()
//        val protocol = JT808Protocol()
//        protocol.messageSerialNo = message.messageSerialNo
//        protocol.messageType = message.messageType
//        protocol.isSubPacket = false
//        protocol.simNo = message.simNo
//        protocol.messageBody = message.writeToBytes()
//        return protocol.writeToBytes().encodeBase64ToString()
    }
}